import Vue from 'vue'
import VueRouter from 'vue-router'

const routerPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
    return routerPush.call(this, location).catch(error => error)
}


Vue.use(VueRouter)

const routes = [
    {
        path: '/',
        name: 'home',
        meta: {
            title: '首页',
            login: 'home',
        },
        component: () => import('../views/home')
    },
    {
        path: '/login',
        name: 'login',
        meta: {
            title: '登录',
            login: 'login'
        },
        component: () => import('../views/login/login')
    },
    {
        path: '/register',
        name: 'register',
        meta: {
            title: '注册',
            login: 'login'
        },
        component: () => import('../views/login/register')
    },
    {
        path: '/RetrievePassword',
        name: 'forgetpass',
        meta: {
            title: '找回密码',
            login: 'login'
        },
        component: () => import('../views/login/RetrievePassword')
    },
    {
        path: '/searchInfo',
        name: 'search',
        meta: {
            title: '搜索',
            login: 'search'
        },
        component: () => import('../views/searchInfo')
    },
    {
        path: '/groupHome',
        name: 'groups',
        meta: {
            title: '分类列表',
            login: 'groups'
        },
        component: () => import('../views/groupHome')
    },
    {
        path: '/groupTwoHome',
        name: 'filterGroups',
        meta: {
            title: '分类筛选',
            login: 'groups'
        },
        component: () => import('../views/groupTwoHome')
    },
    {
        path: '/productDetails',
        name: 'productDetails',
        meta: {
            title: '产品详情',
            login: 'groups'
        },
        component: () => import('../views/productDetails')
    },
    {
        path: '/shoppingCart',
        name: 'cart',
        meta: {
            title: '购物车',
            login: 'car'
        },
        component: () => import('../views/shoppingCart')
    },
    {
        path: '/confirmOrder',
        name: 'confirmOrder',
        meta: {
            title: '确认订单',
            login: 'search'
        },
        component: () => import('../views/confirmOrder')
    },
    {
        path: '/orderInfo',
        name: 'orderInfo',
        meta: {
            title: '订单详情',
            login: 'search'
        },
        component: () => import('../views/orderInfo')
    },

    {
        path: '/PersonalCenter',
        name: 'ucenter',
        redirect: {
            title: '个人中心',
            path: '/PersonalCenter/UserInfo'
        },
        component: () => import('../views/PersonalCenterInfo/Index'),
        children: [

            {
                path: '/PersonalCenter/UserInfo',
                name: 'userInfo',
                meta: {
                    title: '个人中心',
                    login: 'PersonalCenter',
                    type: 'user'
                },
                component: () => import('../views/PersonalCenterInfo/components/UserInfo')
            },
            {
                path: '/PersonalCenter/integralView',
                name: 'integral',
                meta: {
                    title: '积分',
                    login: 'PersonalCenter', type: 'integral'
                },
                component: () => import('../views/PersonalCenterInfo/components/integralView')
            },
            {
                path: '/PersonalCenter/userAddress',
                name: 'address',
                meta: {
                    title: '地址',
                    login: 'PersonalCenter', type: 'address'
                },
                component: () => import('../views/PersonalCenterInfo/components/userAddress')
            },
            {
                path: '/PersonalCenter/userInvoice',
                name: 'invoice',
                meta: {
                    title: '发票',
                    login: 'PersonalCenter',
                    type: 'invoice'
                },
                component: () => import('../views/PersonalCenterInfo/components/userInvoice')
            },
            {
                path: '/PersonalCenter/orderList',
                name: 'orderList',
                meta: {
                    title: '订单',
                    login: 'PersonalCenter',
                    type: 'order'
                },
                component: () => import('../views/PersonalCenterInfo/components/orderList')
            },
            {
                path: '/PersonalCenter/quotationList',
                name: 'quotation',
                meta: {
                    title: '报价单',
                    login: 'PersonalCenter',
                    type: 'offer'
                },
                component: () => import('../views/PersonalCenterInfo/components/quotationList')
            },
            {
                path: '/PersonalCenter/QuotationDetail',
                name: 'quotationDetail',
                meta: {
                    title: '报价单详情',
                    login: 'PersonalCenter', type: 'offer'
                },
                component: () => import('../views/PersonalCenterInfo/components/QuotationDetail')
            },
        ]
    },
    {
        path: '/TechnologyCenter',
        name: 'tcenter',
        meta: {
            title: '技术中心',
            login: 'technology'
        },
        component: () => import('../views/TechnologyCenter')
    },
    {
        path: '/ServiceCenter',
        name: 'scenter',
        meta: {
            title: '服务中心',
            login: 'service'
        },
        component: () => import('../views/ServiceCenter')
    },
    {
        path: '/ShoppingDirectory',
        name: 'directory',
        meta: {
            title: '购物指南',
            login: 'directory'
        },
        component: () => import('../views/ShoppingDirectory')
    },
    {
        path: '/CharacteristicService',
        name: 'characteristic',
        meta: {
            title: '特色服务',
            login: 'characteristic'
        },
        component: () => import('../views/CharacteristicService')
    },
    {
        path: '/AboutUs',
        name: 'about',
        meta: {
            title: '关于我们',
            login: 'about'
        },
        component: () => import('../views/AboutUs')
    },
    {
        path: '/recruit',
        name: 'recruit',
        meta: {
            title: '人才招聘',
            login: 'recruit'
        },
        component: () => import('../views/recruit')
    },
    {
        path: '/contactUs',
        name: 'contact',
        meta: {
            title: '联系我们',
            login: 'contactUs'
        },
        component: () => import('../views/contactUs')
    },
]

const router = new VueRouter({
    mode: 'history',
    base: process.env.BASE_URL,
    routes,
});

router.beforeEach((to, from, next) => {
    window.scrollTo(0, 0)
    let title = "协科光电";
    document.getElementsByTagName('title')[0].innerText = title + '-' + to.meta.title
    next()
})

export default router;
